import { Component } from 'vue-property-decorator';

import { ListViewContext, getBehaviorByKey } from '../../vendors/handie';

import ViewHeadlessWidget from './View';

@Component
export default class ListViewHeadlessWidget extends ViewHeadlessWidget<ListViewContext> {
  protected dataSource: any[] = [];

  protected pageNum: number = 1;

  protected pageSize: number = 20;

  protected total: number = 0;

  private initPagenation(): void {
    const defaultPageSize =
      this.config.defaultPageSize || getBehaviorByKey('common.view.listViewDefaultPageSize', 20);

    this.pageSize = defaultPageSize;

    this.context.setCurrentPage(this.pageNum, true);
    this.context.setPageSize(defaultPageSize, true);
  }

  protected created(): void {
    this.initPagenation();

    this.on({
      dataChange: dataSource => (this.dataSource = dataSource),
      totalChange: total => (this.total = total),
      currentPageChange: pageNum => (this.pageNum = pageNum),
      pageSizeChange: pageSize => (this.pageSize = pageSize),
    });
  }
}
